package ru.tomsk.autovokzal.activity.adapter;

import java.util.ArrayList;
import java.util.List;

import ru.tomsk.autovokzal.R;
import ru.tomsk.autovokzal.tools.TimeTableItem;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;

public class TimeTableAdapter extends ArrayAdapter<TimeTableItem> {

	public static class TimeTableViewHolder {
		public TextView price_text;
		public TextView day_of_following_text;
		public TextView depature_time_text;
		public TextView end_point_text;
		public TextView start_point_text;
		public TextView number_of_route_id_text;
		public TimeTableItem item;

	}

	private static final int TIME_TABLE_ITEM = R.layout.time_table_item;
	private final List<TimeTableItem> originalArray;
	private final Context context;
	private TimeTableItem item;
	private TimeTableViewHolder holder;
	private String TAG = TimeTableAdapter.class.getCanonicalName();
	private ArrayList<TimeTableItem> filterArray;
	private Filter filter;

	public TimeTableAdapter(Context context, List<TimeTableItem> objects) {
		super(context, TIME_TABLE_ITEM, objects);
		this.context = context;
		this.originalArray = objects;
		filterArray = new ArrayList<TimeTableItem>(originalArray);

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		item = originalArray.get(position);
		if (convertView == null) {
			holder = new TimeTableViewHolder();
			LayoutInflater inflate = LayoutInflater.from(context);
			convertView = inflate.inflate(TIME_TABLE_ITEM, null);
			holder.price_text = (TextView) convertView
					.findViewById(R.id.price_text);
			holder.day_of_following_text = (TextView) convertView
					.findViewById(R.id.day_of_following_text);
			holder.depature_time_text = (TextView) convertView
					.findViewById(R.id.depature_time_text);
			holder.end_point_text = (TextView) convertView
					.findViewById(R.id.end_point_text);
			holder.start_point_text = (TextView) convertView
					.findViewById(R.id.start_point_text);
			holder.number_of_route_id_text = (TextView) convertView
					.findViewById(R.id.number_of_route_id_text);
			try {
				holder.item = (TimeTableItem) item.clone();
			} catch (CloneNotSupportedException e) {
				// TODO Auto-generated catch block
				Log.e(TAG, e.getMessage(), e);
			}

		} else {
			holder = (TimeTableViewHolder) convertView.getTag();
		}

		holder.day_of_following_text.setText(item.getDayOfFollowing());
		holder.depature_time_text.setText(item.getDepatureTime());
		holder.end_point_text.setText(item.getEndPoint());
		holder.start_point_text.setText(item.getStartPoint());
		holder.number_of_route_id_text.setText(item.getNumberOfRoute());
		holder.price_text.setText(item.getPrice() + "");
		convertView.setTag(holder);
		return convertView;
	}

	@Override
	public Filter getFilter() {
		if (filter == null)
			filter = new TimeTableFilter();
		return filter;
	}

	public class TimeTableFilter extends Filter {

		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			constraint = constraint.toString().toLowerCase().trim();

			FilterResults result = new FilterResults();

			if (constraint != null && constraint.toString().length() > 0) {

				List<TimeTableItem> founded = new ArrayList<TimeTableItem>();

				for (TimeTableItem t : filterArray) {

					if (t.find(constraint.toString()))

						founded.add(t);
				}

				result.values = founded;
				result.count = founded.size();

			} else {

				result.values = filterArray;
				result.count = filterArray.size();
			}

			return result;
		}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			clear();
			for (TimeTableItem o : (List<TimeTableItem>) results.values) {
				add(o);
			}
			notifyDataSetChanged();

		}

	}
}
